import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;

class Solution {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    static ArrayList<Integer> arrayList = new ArrayList<>();

    public static int[] reversePrint(ListNode head) {
        recur(head);
        int len = arrayList.size();
        int[] a = new int[len];
        for (int i = 0; i < len; i++) {
            a[i] = arrayList.get(i);
        }
        return a;
    }

    public static void recur(ListNode head) {
        if (head == null) return;
        recur(head.next);
        arrayList.add(head.val);
    }

    public static int[] reversePrint1(ListNode head) {
        Stack<Integer> stack = new Stack<>();
        ListNode cur = head;
        while (cur != null) {
            stack.push(cur.val);
            cur = cur.next;
        }
        int len = stack.size();
        int[] a = new int[len];
        for (int i = 0; i < len; i++) {
            a[i] = stack.pop();
        }
        return a;
    }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(3);
        ListNode listNode3 = new ListNode(2);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        System.out.println(Arrays.toString(reversePrint(listNode1)));
    }
}